# plackup -I lib -I ../../SOCK/SOCK/lib --port 6666 --app script/isock.app --daemonize
# starman -I lib -I ../../SOCK/CODE/lib --port 6666 --app script/isock.app --daemonize --pid ../ISOCK_LOGS/isock-RFOLXTS01-6666.pid --error-log ../ISOCK_LOGS/isock-RFOLXTS01-6666.log

use strict;

use Authen::Simple::LDAP;
use iSOCK;
use Plack::Builder;

builder {
 #enable 'Log4perl', category => 'plack', conf => 'log4perl.conf';
 
  enable 'Static', root => 'root', path => qr/ ^ \/ ( css | images | js | lib | src ) \/ /x;
  enable 'Deflater';
  
 #enable 'Auth::Basic', authenticator => \&authenticate, realm => 'iSOCK';
 
  if ( $ENV{ISOCK_DEVEL} ) {
    enable 'Debug';              # Environment, Response, Timer, Memory, Session and DBITrace
    enable 'Debug::CatalystLog';
    enable 'Debug::Parameters';
    
   #enable 'InteractiveDebugger';
  }
  
  iSOCK->psgi_app ( @_ );
};

sub authenticate {
  my ( $login, $password ) = @_;
  
  my $ldap = Authen::Simple::LDAP->new (
    host   => 'rfodc01.rfo.atmel.com',
    binddn => 'CORP\\rfoflowweb',
    bindpw => 'Mdp4fl0ww3b',
    basedn => 'dc=corp, dc=atmel, dc=com',
    filter => '(&(objectClass=organizationalPerson)(objectClass=user)(sAMAccountName=%s))',
  );

  return $ldap->authenticate ( $login => $password ) if $ldap;
  
  return;
}

# Logging
# Status Code Redirect
# Error Handler
# Cache Middleware
# Session Middleware
# Routes Middleware
# App
